const native = require('native')
const sql = require('sql')

cc.Class({
    extends: cc.Component,

    properties: {
        label: {
            default: null,
            type: cc.Label
        },
        cppBtn: {
            default: null,
            type: cc.Button
        },
        javaBtn: {
            default: null,
            type: cc.Button
        },
        ocBtn: {
            default: null,
            type: cc.Button
        },
        sqlBtn: {
            default: null,
            type: cc.Button
        },
        cppLabel: {
            default: null,
            type: cc.Label
        },
        javaLabel: {
            default: null,
            type: cc.Label
        },
        ocLabel: {
            default: null,
            type: cc.Label
        },
        sqlLabel: {
            default: null,
            type: cc.Label
        },
        // defaults, set visually when attaching this script to the Canvas
    },

    // use this for initialization
    onLoad: function () {
        this.cppBtn.node.on('click', this.callNative, this);
        this.javaBtn.node.on('click', this.callNative, this);
        this.ocBtn.node.on('click', this.callNative, this);
        this.sqlBtn.node.on('click', this.callSql, this);
    },

    // called every frame
    update: function (dt) {

    },

    callNative: function (event) {
        cc.log(event.detail.name)
        //cc.log(native)
        var self = this
        native.testCallback = function(arg){
            cc.log(arg)
            
            let platform = native.platform
            switch (platform) {
                case cc.sys.ANDROID:
                self.javaLabel.string = "android:"+arg
                break;
                case cc.sys.IPHONE:
                self.ocLabel.string = "ios:"+arg
                break;
                default:
                self.cppLabel.string = "other:"+arg
                break;
            }
        }
        native.testNative(123)
    },

    callSql: function (event) {
        let isopen = sql.testsql()
        this.sqlLabel.string = "open status:"+isopen;
    },
});
